//
//  HELCreditRecordListVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/27.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELCreditRecordListVC.h"
#import "HELCreditRecordCell.h"
#import "HELCireditRecordModel.h"
#import "XLPhotoBrowser.h"
@interface HELCreditRecordListVC ()<XLPhotoBrowserDelegate>

@property (nonatomic,  assign) BOOL isFirstGetData;
@end

@implementation HELCreditRecordListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isFirstGetData = YES;
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64-44);
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"HELCreditRecordCell" bundle:nil] forCellReuseIdentifier:@"HELCreditRecordCell"];
    
    
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf getCreditRecordList:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
       [weakSelf getCreditRecordList:NO];
    }];
    
    
}

- (void)requestDataFirst{
    if (self.isFirstGetData) {
        [self getCreditRecordList:YES];
        self.isFirstGetData = NO;
    }
}

- (void)getCreditRecordList:(BOOL)isRecover{
   
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(page);
    params[@"rows"] = @(15);
    
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] POST:helCreditRecord parameters:params success:^(id responseObject) {
        
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELCireditRecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            
            [weakSelf.tableView reloadData];
            
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    

    
    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  116;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELCreditRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELCreditRecordCell"];
    
    HELCireditRecordModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    weakSelf_define;
    cell.checkAction =  ^(){
        // 快速创建并进入浏览模式
        XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithCurrentImageIndex:0 imageCount:1 datasource:weakSelf];
        browser.highQualityImageURLForIndex = ^(NSInteger index){
            return [NSURL URLWithString:model.fileSrc?model.fileSrc:@""];
        };

        // 设置长按手势弹出的地步ActionSheet数据,不实现此方法则没有长按手势
        [browser setActionSheetWithTitle:@"保存图片" delegate:weakSelf cancelButtonTitle:nil deleteButtonTitle:nil otherButtonTitles:@"保存图片",nil];
        
        // 自定义一些属性
        browser.showPageControl = NO;
        browser.pageControlStyle = XLPhotoBrowserPageControlStyleAnimated;///< 修改底部pagecontrol的样式为系统样式,默认是弹性动画的样式
        [browser show];
    
    };
    
    return cell;
    
    
}
#pragma mark    -   XLPhotoBrowserDelegate

- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex
{
   
     NSLog(@"点击了actionSheet索引是:%zd , 当前展示的图片索引是:%zd",actionSheetindex,currentImageIndex);
     [browser saveCurrentShowImage];
     
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];


}


@end
